package parser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class Yotc8500Parser implements IParser {

	@Override
	public void parse(String filePath) {
		
		/*格式如：
		 GuLiYOTC(config)# show mac all
		Index       MacAddr         VLAN      State     Port-OnuId
		----------------------------------------------------------------
		 1       70:65:82:72:0d:60  3009      Dynamic   6/3
		 2       3c:8c:40:12:f1:01   667      Dynamic   6/2
		 3       00:1d:08:a0:2d:7c   228      Dynamic   4/3/2
		--More--             920     00:12:d6:3f:6b:42  1000      Dynamic   1/4/15	 
		 */
		File file=new File(filePath);
		String olt = file.getName();
		olt = olt.substring(0,olt.indexOf(".log"));
		File file1=new File(filePath+".out");
		try {
			if(!file.exists()||file.isDirectory())
			    throw new FileNotFoundException();
			
			
			FileWriter fw=new FileWriter(file1);
			BufferedReader br=new BufferedReader(new FileReader(file));
			String temp=br.readLine();
			String[] onus;
			String pon="";			
			String reg = "^\\d{1,2}/\\d{1,2}/\\d{1,2}.*";
			while(temp!=null){
				if (temp.indexOf("Dynamic") > 0) {
					onus = temp.trim().split("\\s+"); //以n个空格切分
					if (temp.indexOf("More")>0) {
						if (onus[7].matches(reg)) fw.write("id:"+onus[3]+",olt:"+olt+",eoc mac:"+onus[4]+",getOnuPort:" + onus[7]+"\n");
					} else {
						if (onus[4].matches(reg)) fw.write("id:"+onus[0]+",olt:"+olt+",eoc mac:"+onus[1]+",getOnuPort:" + onus[4]+"\n");
					}	
				}	
			    temp=br.readLine();
			}
			br.close();
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("deal "+filePath+" error!" );
		} 
    }	

	public Vector<String> preParse(String midData) {
		return null;
	}
	
	public static void main(String[] args) {
		new Yotc8500Parser().parse("E:\\workspace\\javaTelenet\\bin\\output\\output\\10.3.95.242.log");
//		String temp = " --More--             141     38:fa:ca:7f:f9:3b   228      Dynamic   3/4/12";
//		String[] onus = temp.trim().split("\\s+");
//		for (int i = 0; i < onus.length; i++) {
//			System.out.println(i+":"+onus[i]);
//		}
	}
}
